package dk.flashnotes.fffExample.parts {
	import org.osflash.thunderbolt.Logger;	
	
	import dk.flashnotes.fffExample.parts.filterList.FilterListItemEvent;	
	import dk.flashnotes.fff.page.part.APart;
	import dk.flashnotes.fff.utils.XMLDataHolder;
	import dk.flashnotes.fffExample.parts.filterList.FilterListItem;
	import dk.flashnotes.fffExample.text.Label;

	import flash.display.Sprite;		

	/**
	 * @author jk
	 */
	public class ParameterList extends APart {

		private var _doHeadLabel : Label;
		private var _sprListCont : Sprite;
		private var _a_doItems : Array;
		private var _doFLISelected : FilterListItem;

		public function ParameterList(sPartID : String = "", xmlPartData : XML = null) {
			super(sPartID, xmlPartData);
		}

		
		override protected function onPartCreatedAndRegisterd() : void {
			super.onPartCreatedAndRegisterd();
			
			_sprListCont = new Sprite();
			addChild(_sprListCont);
			
			_sprListCont.x = 100;
			_sprListCont.y = 150;
			
			_doHeadLabel = new Label(oPartData.get_sParmByKey("label"));
			_sprListCont.addChild(_doHeadLabel);
			
			_a_doItems = new Array();
			
			addFLI(new FilterListItem(new XMLDataHolder(<item label="All"/>)));
			
			for each (var xmlItem : XML in oPartData.xmlData.item) {
				var _doFLI : FilterListItem = new FilterListItem(new XMLDataHolder(xmlItem));
				addFLI(_doFLI);
			}
			
			selectFLI(get_doFLIByValue(oPage.oTN.oNodeParameterContol.get_sParameterValue(oPartData.get_sParmByKey("key"), "All")));
		}
		
		private function get_doFLIByValue(sValue : String) : FilterListItem {
			
			Logger.info("get fli : " + sValue);
			
			for each (var doFLI : FilterListItem in _a_doItems) {
				if(doFLI.sLabel == sValue) {
					return doFLI;
				}
			}
			return null;
		}		
		
		private function addFLI(_doFLI : FilterListItem) : void {
				
			_doFLI.y = _a_doItems.length * 15 + 20;
			_doFLI.x = 5;
				
			_doFLI.addEventListener(FilterListItemEvent.etREQUEST_SELECTION, onFLIRequest, false, 0, true);

			_a_doItems.push(_doFLI);
			_sprListCont.addChild(_doFLI);
		}

		private function onFLIRequest(event : FilterListItemEvent) : void {
			
			var doFLI : FilterListItem = FilterListItem(event.target);
			selectFLI(doFLI);

		}
		
		private function selectFLI(doFLI : FilterListItem) : void {
			
			if (!doFLI) {
				return;
			}
			
			_oPage.oTN.oNodeParameterContol.setParmameter(oPartData.get_sParmByKey("key"), doFLI.sLabel);
			
			if (_doFLISelected) {
				_doFLISelected.deSelect();
			}
			
			doFLI.select();
			_doFLISelected = doFLI;
		}	}
}
